package com.zuoshenge.user.controller;

import com.zuoshenge.bean.Result;
import com.zuoshenge.user.entity.TUser;
import com.zuoshenge.user.service.TUserService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (TUser)表控制层
 *
 * @author makejava
 * @since 2024-03-15 23:22:06
 */
@RestController
@RequestMapping("tUser")
@CrossOrigin(origins = "http://localhost:8080")
public class TUserController {
    /**
     * 服务对象
     */
    @Resource
    private TUserService tUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<TUser> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.tUserService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param tUser 实体
     * @return 新增结果
     */
    @PostMapping("/adduser")
    public Result<TUser> add(@RequestBody TUser tUser) {
        return Result.ok(this.tUserService.insert(tUser));
    }

//    @PostMapping("/adduser")
//    public String add(@RequestBody TUser tUser, Model model) {
////        return Result.ok(this.tUserService.insert(tUser));
//        this.tUserService.insert(tUser);
//        model.addAttribute("user", tUser);
//        return "index";
//    }

    /**
     * 编辑数据
     *
     * @param tUser 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<TUser> edit(TUser tUser) {
        return ResponseEntity.ok(this.tUserService.update(tUser));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.tUserService.deleteById(id));
    }

}

